import plugin from "../../lib/plugins/plugin.js";
import {
    segment
} from "icqq";
const betMultiplier = {
    '小': 2,
    '大': 2,
    '单': 2,
    '双': 2,
    '豹子': 10
};

var bets = []; // 存储每次押注的信息
var players = [];

// 定时器
let blackjackTimer = {};

export class example extends plugin {
    constructor() {
        super({
            name: '猜猜猜',
            dsc: '猜骰子游戏',
            event: 'message',
            priority: 5000,
            rule: [{
                reg: "^#?开(始|启)猜猜猜$",
                fnc: "startRun"
            }, {
                reg: '^#?押.*金币$',
                fnc: 'XCrossingDice'
            }]
        })
    }
    async XCrossingDice(e) {
        let pattern = /押([\u4e00-\u9fa5]{1,4})/;
        let match = e.msg.match(pattern);
        let pattern1 = /(\d+)/;
        let match1 = e.msg.match(pattern1);
        players.push({
            name: e.user_id,
            coins: 1000000,
            betType: match[1].split(''),
            betAmount: match1[1],
            feePercentage: 10
        });
        for (let player of players) {
            placeBet(player.name, parseInt(player.coins) - parseInt(player.betAmount), player.betType, player.betAmount, 10);
        }
    }
    async startRun() {
        setInterval(() => {
            playRound();
        }, 180000); // 轮次间隔3分钟
    }
    async playRound() {
        var totalCoinsAwarded = 0;
        var dice1, dice2, dice3, sizeAttribute, oddEvenAttribute, leopardAttribute;
        for (let bet of bets) {
            var betType = bet.betType;
            var betAmount = bet.betAmount;
            var feePercentage = bet.feePercentage;

            dice1 = rollDice();
            dice2 = rollDice();
            dice3 = rollDice();
            var sum = dice1 + dice2 + dice3;
            if (sum >= 4 && sum <= 10) {
                sizeAttribute = '小';
            } else if (sum >= 11 && sum <= 17) {
                sizeAttribute = '大';
            }

            if (sum % 2 === 0) {
                oddEvenAttribute = '双';
            } else {
                oddEvenAttribute = '单';
            }

            if (dice1 === dice2 && dice2 === dice3) {
                leopardAttribute = '豹子';
            }

            let result;
            if (betType.includes(sizeAttribute)) {
                result = sizeAttribute;
            }
            if (betType.includes(oddEvenAttribute)) {
                result = oddEvenAttribute;
            }
            if (betType.includes(leopardAttribute)) {
                result = leopardAttribute;
            }

            // 遍历押注者数组，根据押注信息进行判断，给赢家加金币
            for (let player of players) {
                if (player.name === bet.playerName) {
                    let winAmount = 0;
                    let fee = 0;
                    for (let type of bet.betType) {
                        if (type === result) {
                            winAmount += betMultiplier[type] * bet.betAmount;
                        }
                    }
                    fee = winAmount - (winAmount * bet.feePercentage / 100);
                    player.coins += parseInt(fee);
                    totalCoinsAwarded += parseInt(fee);
                    // console.log(`${player.name}，恭喜你，猜对了！三个骰子的点数分别为${dice1}、${dice2}、${dice3}，属性组合是${sizeAttribute}${oddEvenAttribute}，您赢得了${parseInt(fee)}金币！`);
                }
            }
        }
        console.log(`${dice1}、${dice2}、${dice3},${sizeAttribute}${oddEvenAttribute},本轮共发放${totalCoinsAwarded}金币`);
        // 清空本轮的押注信息
        bets = [];
        players = [];
    }
    async rollDice() {
        return Math.floor(Math.random() * 6) + 1;
    }

    // 修改 placeBet 函数，接受押注者的名称
    async placeBet(playerName, coins, betType, betAmount, feePercentage) {
        bets.push({
            playerName,
            coins,
            betType,
            betAmount,
            feePercentage
        });
    }
}